#!/bin/sh /etc/rc.common
# Copyright (C) 2007-2011 OpenWrt.org

# shellcheck disable=SC2034
START=99
# shellcheck disable=SC2034
STOP=16
# shellcheck disable=SC2034
USE_PROCD=1

NAME=cells
PROG="/usr/bin/$NAME"

CONFIG_ROOT=/etc/pydio
PIDFILE=/var/run/cells.pid
workingdir=$(uci -q get cells.general.workingdir)
bindip=$(uci -q get cells.general.bindip)
bindport=$(uci -q get cells.general.bindport)
external=$(uci -q get cells.general.external)

if [ -z "$bindip" ]; then
   bindip="0.0.0.0"
fi

if [ -z "$bindport" ]; then
   bindport="8080"
fi

if [ -z "$workingdir" ]; then
   workingdir="/root"
fi

LOGGER="/usr/bin/logger -p user.err -s -t $NAME msg:"
[ -x "$LOGGER" ] || LOGGER="echo"

cells_kill() {
	# get all running pids and kill client connections
	local skip
	for pid in $(pidof "${PROG}")
	 do
		# kill process
		# echo "CELLS: Killing ${pid}..."
		kill -KILL ${pid}
	done
}


cells_status() {
	ps_alive=0
	pid=$(pidof "${PROG}")
	if [ -n "$pid" ]; then
		ps_alive=1
	fi

	if { [ "$1" = check_alive ] && [ $ps_alive = 1 ]; } || \
		{ [ "$1" = check_dead ] && [ $ps_alive = 0 ]; }
	then
	    #$LOGGER "$PROG is already running"
		return 0 # EXIT_SUCCESS
	else
	    #$LOGGER "$PROG is not running"
		return 1 # EXIT_FAILURE
	fi
}

start_service() {
	# Get various config options
	config_load "$NAME"
	config_get my_user general user "pydio"
	config_get my_group general group "pydio"
	config_get_bool enabled general enabled 1
	config_get_bool init_site general init 1
	config_get_bool autoupgrade general upgrade 1
	config_get options general options
	
	# Few basic checks
	if [ ! -x "$PROG" ]; then
		$LOGGER "$PROG is missing"
		exit 1
	fi
	
	if cells_status check_alive; then
		$LOGGER "$PROG is already running"
		exit 0
	fi

	# shellcheck disable=SC2154
	if [ "$enabled" -eq 0 ]; then
		$LOGGER "service not enabled in /etc/config/$NAME"
		exit 1
	fi
	
	if [ ! -d "$workingdir" ]; then
		mkdir -p -m 0777 $workingdir
	fi

	#$LOGGER "$PROG is ready start"
	
	# Start daemon
	procd_open_instance $NAME

	# shellcheck disable=SC2154 disable=SC2086
	procd_set_param command "$PROG"
	
	# Make sure we have a working database in datadir
	if [ ! -f "$workingdir/pydio.json" ]; then
		if [ "$init_site" -eq 1 ]; then
		    $LOGGER "Cells Server: param is configure"
			procd_append_param command "configure"
		else
		    $LOGGER "Cells Server: plase set init_site 1!"
			exit 0;
		fi
    else
		$LOGGER "Cells Server: param is start"
		procd_append_param command "start"
	fi

	procd_set_param file /etc/config/cells
	procd_set_param pidfile $PIDFILE
	
	# run as user
	##procd_set_param user "root"
	# forward stderr to logd
	procd_set_param stderr 1
	# use HUP to reload
	##procd_set_param reload_signal HUP
	# terminate using signals
	procd_set_param term_timeout 60

	procd_set_param env CELLS_WORKING_DIR=${workingdir} \
	CELLS_BIND=${bindip}:${bindport} \
	CELLS_EXTERNAL=${external}
	
	procd_set_param respawn
	procd_close_instance
	
	#$LOGGER "Cells Server is start done"
}

stop_service() {
    cells_kill  >&2
    $LOGGER "Cells Server has stopped."  >&2
}
